<?php
/**
 * 数据源管理
 * 
 * @author river_he@eueang.com
 */
final class Kit_Db_Manager {
	/**
	 * 连接参数
	 * 
	 * @var array
	 */
	private static $_config = null;
	
	/**
	 * 连接源组
	 * 
	 * @var array
	 */
	private static $_adapters = array();
	
	/**
	 * 设置数据库连接参数
	 * 
	 * @param array $config
	 */
	public static function setConfig(array $config){
		if(self::$_config === null){
			self::$_config = $config;
		}
	}
	
	/**
	 * 获取数据源
	 * 
	 * @param string $dbIdenty 数据源标识
	 * @return Kit_Db_Adapter
	 */
	public static function getAdapter($dbIdenty){
		$return = null;
		if(array_key_exists($dbIdenty, self::$_adapters)){
			$return = self::$_adapters[$dbIdenty];
		}else if(array_key_exists($dbIdenty, self::$_config)){
			$return = self::crateAdaper(self::$_config[$dbIdenty]);
			if($return instanceof Kit_Db_Adapter){
				self::$_adapters[$dbIdenty] = $return;
			}else{
				throw new Kit_Db_Exception('The DbAdapter Install Failure.', 501);
			}
		}else{
			throw new Kit_Db_Exception('No the match db server in config file, please check it again.', 404);
		}
		return $return;
	}
	
	/**
	 * 创建连接源
	 * 
	 * @param array $config
	 * @return Kit_Db_Adapter
	 */
	private static function crateAdaper(array $config){
		$return = new Kit_Db_Adapter($config);
		return $return;
	}
}
//end